import urllib.request



def load_data():
    url = "http://www.baidu.com/"

    #get请求
    response = urllib.request.urlopen(url)
    print(response)

    #读取内容
    data = response.read()
    str_data = data.decode('utf-8')  #解码

    #写入文件
    with open('baidu.html','w',encoding='utf-8') as file:
        file.write(str_data)

    #将字符串类型转换成bytes
    str_name = 'baidu'
    bytes_name = str_name.encode('utf-8')
    print(bytes_name)

    #如果爬取的是bytes  写入的时候decode
    #如果爬取的是str    写入的是bytes时候encode


load_data()